//定义一些对象
var updatePositionParam = new Object();
var departmentParam = new Object();
$(function() {
	$('#organization').tree({
		url : basePath + 'organization/getOrganizationTreeData.do',
		animate : true,
		lines : true,
		onContextMenu: function(e, node){
			e.preventDefault();
			// 查找节点
			$('#organization').tree('select', node.target);
			var isLeaf = $('#organization').tree('isLeaf', node.target);
			if(isLeaf){	//子节点
				// 显示快捷菜单
				$('#mm1').menu('show', {
					left: e.pageX,
					top: e.pageY
				});
				var parentNode = $('#organization').tree('getParent', node.target);
				var department = parentNode.text;
				var pre_position = node.text;
				//修改职位
				//清空对象
				delete updatePositionParam.department;
				delete updatePositionParam.pre_position;
				//给对象赋值
				updatePositionParam.department = department;
				updatePositionParam.pre_position = pre_position;
			}else{	//父节点
				// 显示快捷菜单
				$('#mm2').menu('show', {
					left: e.pageX,
					top: e.pageY
				});
				var pre_department = node.text;
				//清空对象
				delete departmentParam.pre_department;
				//给对象赋值
				departmentParam.pre_department = pre_department;
			}
		}

		
	})
})

//增加部门
function addDepartment() {
	$.messager.prompt('增加部门', '请输入部门名称：', function(r){
		if (r){
			$.ajax({
				type : 'POST',
				url : basePath + 'organization/addDepartment.do',
				data : {
					'department' : r
				},
				dataType : 'json',
				success: function(msg){
					if(msg.result==true){
						$.messager.show({
							title:'提示',
							msg:'增加部门成功!',
							showType:'show',
							timeout:1500,
							style:{
								right:'',
								top:document.body.scrollTop+document.documentElement.scrollTop,
								bottom:''
							}
						});
						var array = getRootsState();
						$('#organization').tree('reload');
						var i,size;
						for(i=0,size=array.length;i<size;i++){
							var id = array[i].id;
							var state = array[i].state;
							if(state=='open'){
								var node = $('#organization').tree('find', id);
								$('#organization').tree('expand', node.target);
							}
						}
					}else{
						$.messager.alert('提示','服务器出错,增加失败！','info');
					}
				}
			})
		}
	});
}

//修改职位
function updatePosition(){
	$.messager.prompt('修改职位', '请输入新的职位名称：', function(r){
		if (r){
			var array = getRootsState();
			$.ajax({
				type : 'POST',
				url : basePath + 'organization/updatePosition.do',
				data : {
					'department': updatePositionParam.department,
					'pre_position': updatePositionParam.pre_position,
					'now_position': r
				},
				dataType : 'json',
				success: function(msg){
					if(msg.result==true){
						$.messager.show({
							title:'提示',
							msg:'修改职位成功!',
							showType:'show',
							timeout:1500,
							style:{
								right:'',
								top:document.body.scrollTop+document.documentElement.scrollTop,
								bottom:''
							}
						});
						
						$('#organization').tree('reload');
						
					}else{
						$.messager.alert('提示','服务器出错,修改失败！','info');
					}
				}
			})
			expandNode(array);
		}
	});
}

//修改部门
function updateDepartment(){
	$.messager.prompt('修改部门', '请输入新的部门名称：', function(r){
		if (r){
			$.ajax({
				type : 'POST',
				url : basePath + 'organization/updateDepartment.do',
				data : {
					'pre_department': departmentParam.pre_department,
					'now_department': r
				},
				dataType : 'json',
				success: function(msg){
					if(msg.result==true){
						$.messager.show({
							title:'提示',
							msg:'修改部门成功!',
							showType:'show',
							timeout:1500,
							style:{
								right:'',
								top:document.body.scrollTop+document.documentElement.scrollTop,
								bottom:''
							}
						});
						var array = getRootsState();
						$('#organization').tree('reload');
						var i,size;
						for(i=0,size=array.length;i<size;i++){
							var id = array[i].id;
							var state = array[i].state;
							if(state=='open'){
								var node = $('#organization').tree('find', id);
								$('#organization').tree('expand', node.target);
							}
						}
					}else{
						$.messager.alert('提示','服务器出错,修改失败！','info');
					}
				}
			})
		}
	});
}

//增加职位
function addPosition(){
	$.messager.prompt('增加职位', '请输入新的职位名称：', function(r){
		if (r){
			$.ajax({
				type : 'POST',
				url : basePath + 'organization/addPosition.do',
				data : {
					'department': departmentParam.pre_department,
					'position': r
				},
				dataType : 'json',
				success: function(msg){
					if(msg.result==true){
						$.messager.show({
							title:'提示',
							msg:'增加职位成功!',
							showType:'show',
							timeout:1500,
							style:{
								right:'',
								top:document.body.scrollTop+document.documentElement.scrollTop,
								bottom:''
							}
						});
						$('#organization').tree('reload');
					}else{
						$.messager.alert('提示','服务器出错,增加失败！','info');
					}
				}
			})
		}
	});
}

//获取根节点状态
function getRootsState(){
	var roots=$('#organization').tree('getRoots'),i,size;
	var state = function(id,state){
		this.id = id;
		this.state = state;
	};
	var array = [];
	for(i=0,size=roots.length;i<size;i++){
		var s = new state(roots[i].id,roots[i].state);
		array.push(s);
	}
	return array;
}

//展开节点
function expandNode(array){
	var i,size;
	for(i=0,size=array.length;i<size;i++){
		var id = array[i].id;
		var state = array[i].state;
		if(state=='open'){
			var node = $('#organization').tree('find', id);
			$('#organization').tree('expand', node.target);
		}
	}
}